package org.shuma.user.mng.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.shuma.user.facade.UserFacade;
import org.shuma.user.mng.domain.entity.User;
import org.shuma.user.mng.domain.vo.AuthVo;
import org.shuma.user.mng.mapper.UserMapper;
import org.shuma.user.mng.service.UserService;
import org.shuma.user.mng.utils.BcryptUtil;
import org.shuma.user.mng.utils.BeanCopyUtils;
import org.shuma.user.mng.utils.RedisUtil;
import org.shuma.user.utils.HttpCodeEnum;
import org.shuma.user.utils.Result;
import org.shuma.user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author liuming
 */
@Service
@SofaService(interfaceType = UserFacade.class, uniqueId = "${service.unique.id}", bindings = {@SofaServiceBinding(bindingType = "bolt")})
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService, UserFacade {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<UserVo> getUserInfoByToken(String token) {
        Result<UserVo> result = new Result<>();
        if (redisUtil.getExpire(token) < 0 || Objects.isNull(redisUtil.getUserInfo(token))) {
            result.error(HttpCodeEnum.NEED_LOGIN, "");
            return result;
        }
        User user = redisUtil.getUserInfo(token);
        result.ok(BeanCopyUtils.copyBean(user, UserVo.class));
        return result;
    }

    @Override
    public Result<UserVo> getUserInfoById(Long id) {
        User user = userMapper.selectById(id);
        Result<UserVo> result = new Result<>();
        if (Objects.nonNull(user)) {
            result.ok(BeanCopyUtils.copyBean(user, UserVo.class));
        } else {
            result.error(HttpCodeEnum.NO_FIND, "");
        }
        return result;
    }

    @Override
    public Result updatePassword(AuthVo auth) {
        Result res = new Result<>();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, auth.getUserName());
        User user = userMapper.selectOne(queryWrapper);
        if (Objects.nonNull(user)) {
            if (verify(user.getEmail(), auth.getCode())) {
                user.setUserPassword(BcryptUtil.encode(auth.getNewPassword()));
                userMapper.updateById(user);
                res.ok();
            } else {
                res.error(HttpCodeEnum.CODE_ERROR, "");
            }
        } else {
            res.error(HttpCodeEnum.USER_NOT_NULL, "");
        }
        return res;
    }

    private boolean verify(String email, Integer code) {
        String verifyCode = redisUtil.get(email);
        return Objects.nonNull(verifyCode) && verifyCode.equals(String.valueOf(code));
    }
}




